#!/bin/bash
ASSIGNMENT_NUM=lab4
SIMTRAXROOT=../
SAMPLE_DIR=${SIMTRAXROOT}samples/
SCENE_DIR=${SAMPLE_DIR}scenes

ASSEMBLY=${SAMPLE_DIR}src/pathtracer/rt-llvm.s

NUM_PROCS=32
NUM_ICACHE_BANKS=16
NUM_TM=20
NUM_L2=4
NUM_ICACHES=2
SIM_THREADS=8
RAY_DEPTH=2
NUM_SAMPLES=4
THREADS_PER_PROC=1
CACHE_CONFIG_FILE_NUM=10
# hardware configuration file
CONFIGS=${SAMPLE_DIR}configs/l1conf${CACHE_CONFIG_FILE_NUM}.config


L1_ON=1
DCACHE_AREA_ON=1

L1_CONFIG=""
DCACHE_CONFIG=""
# simulation report file
OUTPUT_FILE=../${ASSIGNMENT_NUM}_l1_${L1_ON}_dcachearea_${DCACHE_CONFIG}_useL1Conf${CACHE_CONFIG_FILE_NUM}_report.txt

OUT_IMG_PREFIX="out"

RENDER_CORNELL=
RENDER_CONFERENCE=

PROFILE=""

CORNELL_OBJ=$SCENE_DIR/cornell/CornellBox.obj
CORNELL_VIEW=$SCENE_DIR/cornell/cornell.view
CORNELL_LIGHT=$SCENE_DIR/cornell/cornell.light

CONFER_OBJ=$SCENE_DIR/conference/conference.obj
CONFER_VIEW=$SCENE_DIR/conference/conference.view
CONFER_LIGHT=$SCENE_DIR/conference/conference.light

SPONZA_NAME=$SCENE_DIR/sponza/sponza

SPONZA_OBJ=${SPONZA_NAME}.obj
SPONZA_VIEW=${SPONZA_NAME}.view
SPONZA_LIGHT=${SPONZA_NAME}.light

updateConfig() {
    
    OUTPUT_FILE=../${ASSIGNMENT_NUM}_raydepth_${RAY_DEPTH}_samplePerPix_${NUM_SAMPLES}_report.txt
    #echo "output file name config finish"

    CONFIGS=${SAMPLE_DIR}configs/l1conf${CACHE_CONFIG_FILE_NUM}.config

    CORNELL_SCENE="--model $CORNELL_OBJ --view-file $CORNELL_VIEW --light-file $CORNELL_LIGHT"

    CONFERENCE_SCENE="--model $CONFER_OBJ --view-file $CONFER_VIEW --light-file $CONFER_LIGHT"

    SPONZA_SCENE="--model $SPONZA_OBJ --view-file $SPONZA_VIEW --light-file $SPONZA_LIGHT"

    SIMTRAXCONF="--load-assembly $ASSEMBLY --num-thread-procs $NUM_PROCS --num-TMs $NUM_TM --num-l2s $NUM_L2 --num-icache-banks $NUM_ICACHE_BANKS --num-icaches $NUM_ICACHES $DCACHE_CONFIG $L1_CONFIG --config-file $CONFIGS $PROFILE --simulation-threads $SIM_THREADS --ray-depth $RAY_DEPTH --num-samples $NUM_SAMPLES --output-prefix $OUT_IMG_PREFIX"


    RENDER_CORNELL="./simtrax $CORNELL_SCENE $SIMTRAXCONF"

    RENDER_CONFERENCE="./simtrax $CONFERENCE_SCENE $SIMTRAXCONF"
    
    RENDER_SPONZA="./simtrax $SPONZA_SCENE $SIMTRAXCONF"
}

#$RENDER_CORNELL
SingleRun () {
    updateConfig
    echo "Render configuration:"
    echo $RENDER_CONFERENCE
    echo "Output file:"
    echo $OUTPUT_FILE
    ${RENDER_CONFERENCE} > $OUTPUT_FILE
    echo "Done."
}

MultiRun () {
    x=1
    NUM_SAMPLES=1
    while [ $x -le 4 ]; do
        RAY_DEPTH=$x
        echo "ray depth ${RAY_DEPTH} running"
        OUT_IMG_PREFIX="out_ray_depth_${RAY_DEPTH}"
        SingleRun
        echo "Progress is $x/4"
        #echo $x
        (( x++ ))
    done

    x=1
    RAY_DEPTH=1
    while [ $x -le 4 ]; do
        NUM_SAMPLES=$x
        echo "num samples ${NUM_SAMPLES} running"
         OUT_IMG_PREFIX="out_samples_${NUM_SAMPLES}"
        SingleRun
        echo "Progress is $x/4"
        #echo $x
        (( x++ ))
    done

}

RunL1Conf16 ()
{
    CACHE_CONFIG_FILE_NUM=16
    echo "config file l1conf${CACHE_CONFIG_FILE_NUM}.config running"
    SingleRun
}

#RunL1Conf16
RAY_DEPTH=4
NUM_SAMPLES=1
echo "ray depth ${RAY_DEPTH} running"
OUT_IMG_PREFIX="out_ray_depth_${RAY_DEPTH}"
SingleRun

#MultiRun
#gthumb ./out.png &
